﻿Public Class frmOvertimeDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManager As New PayrollManager
    Private EmployeeInfo As EmployeeInfo

    Private Sub frmOvertimeDE_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        EmployeeInfo = MainGlobalData.mEmployeeInfo
        txtEmployeeName.Text = EmployeeInfo.last_name & "," & EmployeeInfo.first_name
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                Dim overtimeList As List(Of Overtime) = payrollManager.getOvertimeId(Clipboard.GetData(DataFormats.Text))
                For Each overtime As Overtime In overtimeList
                    dtpDateFrom.Text = overtime.ot_from
                    dtpDateTo.Text = overtime.ot_to
                    txtOvertimeHours.Text = overtime.ot_hours
                    'NOTE: ONLY THE ADMIN CAN VIEW THE CHECKBOX
                    chkAproveOvertime.Checked = (If((overtime.status = "0"), False, True))
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpLeave)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Try
            Dim overtimeObject As New Overtime
            overtimeObject.employee_id = mEmployeeInfo.employee_id
            overtimeObject.ot_from = dtpDateFrom.Text
            overtimeObject.ot_to = dtpDateTo.Text
            overtimeObject.ot_hours = txtOvertimeHours.Text
            'NOTE: ONLY THE ADMIN CAN VIEW THE CHECKBOX
            overtimeObject.status = (If((chkAproveOvertime.Checked), "1", "0"))
            overtimeObject.id = Clipboard.GetData(DataFormats.Text)
            If Clipboard.GetData(DataFormats.Text) = "" Then
                payrollManager.insertOvertime(overtimeObject)
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemOvertime(overtimeObject)
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpLeave)
        Else
            Me.Close()
        End If
    End Sub
End Class